﻿using _8Group.MES.ISongService.WmOutsourceRecpt;
using _8Group.MES.ISongService.WmOutsourceRecpt.DTO;
using _8Group.MES.ISongService.WMProductRecpt;
using _8Group.MES.Model.WM仓储管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.SongService
{
    //[ApiExplorerSettings(GroupName = "宋恒漳")]
    public class WmOutsourceRecptService : ApplicationService, IWmOutsourceRecptService
    {
        private readonly IRepository<WM_Outsource_Recpt, int> _wmOutsourceRecptRepository;
        public WmOutsourceRecptService(IRepository<WM_Outsource_Recpt, int> wmOutsourceRecptRepository)
        {
            _wmOutsourceRecptRepository = wmOutsourceRecptRepository;
        }
        /// <summary>
        /// 获取外协入库列表
        /// </summary>
        /// <param name="applicationDTO"></param>
        /// <returns></returns>
        public async Task<object> GetOutsourceRecptList(RequestIApplicationDTO applicationDTO)
        {
            var wmOutsourceRecptList = await _wmOutsourceRecptRepository.GetQueryableAsync();


            wmOutsourceRecptList = wmOutsourceRecptList.WhereIf(applicationDTO.RecptCode != null, x => x.RecptCode.Contains(applicationDTO.RecptCode));
            wmOutsourceRecptList = wmOutsourceRecptList.WhereIf(applicationDTO.VendorName != null, x => x.VendorName.Contains(applicationDTO.VendorName));
            wmOutsourceRecptList = wmOutsourceRecptList.WhereIf(applicationDTO.RecptDate != null, x => x.RecptDate.ToString().Contains(applicationDTO.RecptDate));
            wmOutsourceRecptList = wmOutsourceRecptList.WhereIf(applicationDTO.WorkorderCode != null, x => x.WorkorderCode.Contains(applicationDTO.WorkorderCode));
            var count = wmOutsourceRecptList.Count();
            wmOutsourceRecptList = wmOutsourceRecptList.OrderBy(x => x.Id).Skip(((applicationDTO.Page - 1) ?? 0) * (applicationDTO.Limit ?? 10)).Take(applicationDTO.Limit ?? 10);
            var result = new ResponseModel
            {
                code = "",
                count = count,
                data = wmOutsourceRecptList.ToList(),
                msg = "",
            };
            return result;
        }


        /// <summary>
        /// 删除外协入库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteOutsourceRecpt(int id)
        {
            var wmOutsourceRecpt = await _wmOutsourceRecptRepository.GetAsync(id);
            await _wmOutsourceRecptRepository.DeleteAsync(wmOutsourceRecpt, true);
            return true;
        }

        /// <summary>
        ///新增外协入库
        /// </summary>
        /// <param name="wmOutsourceRecptDTO"></param>
        /// <returns></returns>
        public async Task<bool> AddOutsourceRecpt(AddWmOutsourceRecptDTO wmOutsourceRecptDTO)
        {
            //判断重名
            var wmOutsourceRecpt = await _wmOutsourceRecptRepository.FirstOrDefaultAsync(x => x.RecptCode == wmOutsourceRecptDTO.RecptCode);
            if (wmOutsourceRecpt != null)
            {
                return false;
            }
            AutoMapperExt.MapTo<AddWmOutsourceRecptDTO, WM_Outsource_Recpt>(wmOutsourceRecptDTO, wmOutsourceRecpt);
            await _wmOutsourceRecptRepository.InsertAsync(wmOutsourceRecpt);
            return true;
        }

        /// <summary>
        ///     更新外协入库
        /// </summary>
        /// <param name="wmOutsourceRecptDTO"></param>
        /// <returns></returns>
        public async Task<bool> UpdateOutsourceRecpt(UpdateWmOutsourceRecptDTO wmOutsourceRecptDTO)
        {
            var wmOutsourceRecpt = await _wmOutsourceRecptRepository.GetAsync(wmOutsourceRecptDTO.Id);
            AutoMapperExt.MapTo<UpdateWmOutsourceRecptDTO, WM_Outsource_Recpt>(wmOutsourceRecptDTO, wmOutsourceRecpt);
            await _wmOutsourceRecptRepository.UpdateAsync(wmOutsourceRecpt);
            return true;
        }
    }
}
